from Crypto import Random
from Crypto.PublicKey import RSA
# 创建秘钥对

rdn = Random.new().read
# 生成秘钥对象
rsa_obj = RSA.generate(1024, rdn)

# 公钥
public_key = rsa_obj.public_key().export_key()
# 将公钥保存到文件中
with open('public.pem', 'wb') as f:
    f.write(public_key)
# 私钥
private_key = rsa_obj.export_key()
with open('private.pem', 'wb') as f:
    f.write(private_key)
